import { post, get } from '@/utils/request'
import siteConfig from '@/config/site'
import { jsonToSearch } from '@/utils/tools'

// 用户登录
export const userLoginApi = (userData: IUserType.ILoginForm) => {
  return post<IUserType.ILoginResult>(`/api/user/login`, userData)
}

// 用户列表
export const getUserListApi = (
  page = 1,
  query: Record<string, any> = {},
  size = siteConfig.pageSize
) => {
  // 一定要对query对象进行一次深复制，然后在去除query中的page字段属性
  let queryObj = { ...query }
  queryObj.page && delete queryObj.page
  let queryString = jsonToSearch(queryObj)
  return get<{ users: IUserType.IUserItem[]; total: number }>(
    `/api/user/list?page=${page}&size=${size}${queryString}`
  )
}

// 用户唯一性验证
export const checkUserExistApi = (username: string) => {
  return post<number>(`/api/user/exist`, { username })
}

// 上传头像
export const uploadUserAvatarApi = (form: FormData) => {
  return post<{ imgUrl: string; filepath: string }>(`/api/user/avatar`, form)
}

// 新增用户
export const addUserApi = (userData: IUserType.IUserForm) => {
  return post<number>(`/api/user/create`, userData)
}

// 批量删除指定用户
export const batchRemoveUserApi = (ids: number[]) => {
  return post<number>(`/api/user/delbatch`, { ids })
}

// 批量获取指定用户id的用户记录
export const getUidsToUsersApi = (ids: number[]) => {
  return post<IUserType.IUserItem[]>(`/api/user/ids`, { ids })
}
